//
//  CellTableView.m
//  QQ聊天列表
//
//  Created by Jorn.Wu on 15/5/23.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "CellTableView.h"
#import "MessageFrameModel.h"
#import "MessageModel.h"
#import "Constant.h"

@interface CellTableView()

//时间
@property (nonatomic, weak)UILabel *time;
//正文
@property (nonatomic, weak)UIButton *textView;
//用户头像
@property (nonatomic, weak)UIImageView *icon;

@end

@implementation CellTableView

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //时间显示
        UILabel *time = [[UILabel alloc]init];
        time.textAlignment = NSTextAlignmentCenter;//居中
        time.font = kTimeFont;
        [self.contentView addSubview:time];
        self.time = time;
        
        //正文
        UIButton *textView = [[UIButton alloc]init];
//        textView.backgroundColor = [UIColor grayColor];
        textView.titleLabel.font = kButtonFont;
        textView.titleLabel.numberOfLines = 0;//自动换行
        
        textView.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);//设置内编剧
        [textView setTitleColor:kColorWithRGB(100, 240, 250, 1) forState:UIControlStateNormal];//字体颜色
        
        [self.contentView addSubview:textView];
        self.textView = textView;
        
        //头像
        UIImageView *icon = [[UIImageView alloc]init];
        [self.contentView addSubview:icon];
        self.icon = icon;
        
        self.backgroundColor = [UIColor clearColor];//清除cell的颜色
    }
    return self;
}

+ (instancetype)messageCellWithTableView:(UITableView *)tabelView{
    static NSString *ID = @"messageCell";
    CellTableView *cell = [tabelView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

- (void)setMessageFrame:(MessageFrameModel *)messageFrame{
    _messageFrame = messageFrame;
    MessageModel *model = messageFrame.message;
//时间
    self.time.frame = messageFrame.timeF;
    self.time.text = model.time;//内容
    
//头像
    self.icon.frame = messageFrame.iconF;
    if (model.type == MessageModelPersonOne) {
        self.icon.image = [UIImage imageNamed:@"Gatsby"];
    }else {
        self.icon.image = [UIImage imageNamed:@"Jobs"];
    }
//正文
    self.textView.frame = messageFrame.textViewF;
    [self.textView setTitle:model.text forState:UIControlStateNormal];
    
    //设置图片
    if (model.type == MessageModelPersonOne) {
        [self.textView setBackgroundImage:[self resizeWithImageName:@"chat_send_nor"] forState:UIControlStateNormal];
    }else {
        [self.textView setBackgroundImage:[self resizeWithImageName:@"chat_recive_nor"] forState:UIControlStateNormal];
    }
}
//拉伸图片的方法
- (UIImage *)resizeWithImageName:(NSString *)name{
    UIImage *normal = [UIImage imageNamed:name];
    
    CGFloat w = normal.size.width * 0.5f;
    CGFloat h = normal.size.height * 0.5f;
    
    return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(w, h, w, h) resizingMode:UIImageResizingModeStretch];
    
//    还可以用下面梁中国方法
//    [normal resizableImageWithCapInsets:UIEdgeInsetsMake(w, h, w, h)];
//    [normal stretchableImageWithLeftCapWidth:w topCapHeight:h]
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
